package cn.e4j.modular.system.controller;

import cn.e4j.config.properties.E4jProperties;
import cn.e4j.core.base.controller.BaseController;
import cn.e4j.core.common.exception.BizExceptionEnum;
import cn.e4j.core.dto.impl.Dto;
import cn.e4j.core.dto.requestDtoResolver.DtoParam;
import cn.e4j.core.dto.requestDtoResolver.DtoWapper;
import cn.e4j.core.reqres.response.ErrorResponseData;
import cn.e4j.core.reqres.response.PageReData;
import cn.e4j.core.reqres.response.ResponseData;
import cn.e4j.core.util.AppCxt;
import cn.e4j.model.exception.ServiceException;
import cn.e4j.modular.system.entity.SysParams;
import cn.e4j.modular.system.service.SysParamsService;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;
import java.util.Map;

/**
 * 全局参数管理
 *
 * @author wenchuan.li
 * @Date 2018/12/08 16:03
 */
@Controller
@RequestMapping("/sys/param")
public class SysParamController extends BaseController {

    @Autowired
    private SysParamsService sysParamsService;

    /**
     * 跳转全局参数管理页面
     */
    @RequiresPermissions("sys:param:init")
    @RequestMapping(value = {"", "init"})
    public String index() {
        return "/pc/system/param.html";
    }

    /**
     * 跳转到查看编辑的页面
     */
    @RequestMapping("/param_add")
    public String addView() {
        return "/pc/system/param_edit.html";
    }

    /**
     * 查询界面组件信息列表
     */
    @RequestMapping("/getParamslist")
    @RequiresPermissions("sys:param:init")
    @ResponseBody
    public ResponseData getParamslist(@DtoParam DtoWapper dtoWapper) {
        Page<Object> page = dtoWapper.getPage();
        Dto inDto = dtoWapper.getDto();
        List<Map<String, Object>> result = sysParamsService.getParamslist(page, inDto);
        return new PageReData(page, result);
    }

    /**
     * 添加
     */
    @RequestMapping("/add")
    @RequiresPermissions("sys:param:init")
    @ResponseBody
    public ResponseData add(SysParams params, BindingResult result) {
        if (result.hasErrors()) {
            throw new ServiceException(BizExceptionEnum.REQUEST_NULL);
        }

        //判断参数key值是否重复
        SysParams theParam = this.sysParamsService.getByKey(params.getCODE());
        if (theParam != null) {
            return new ErrorResponseData(BizExceptionEnum.EXISTED_THE_PARAMS.getCode() ,
                    BizExceptionEnum.EXISTED_THE_PARAMS.getMessage());
        }

        params.insert();
        AppCxt.delParam(params.getCODE());
        return SUCCESS_TIP;
    }

    /**
     * 修改
     */
    @RequestMapping("/edit")
    @RequiresPermissions("sys:param:init")
    @ResponseBody
    public ResponseData edit(SysParams params, @DtoParam DtoWapper dtoWapper) {
        Dto dto=dtoWapper.getDto();
        String code = params.getCODE();
        String oldcode = dto.getString("oldcode");

        if(!code.equals(oldcode)){
            //判断参数key值是否重复
            SysParams theParam = this.sysParamsService.getByKey(params.getCODE());
            if (theParam != null) {
                return new ErrorResponseData(BizExceptionEnum.EXISTED_THE_PARAMS.getCode() ,
                        BizExceptionEnum.EXISTED_THE_PARAMS.getMessage());
            }
            AppCxt.delParam(code);
        }

        AppCxt.delParam(oldcode);

        params.updateById();

        return SUCCESS_TIP;
    }

    /**
     * 删除
     */
    @RequestMapping("/del")
    @RequiresPermissions("sys:param:init")
    @ResponseBody
    public ResponseData del(SysParams params, BindingResult result) {
        params.deleteById();
        AppCxt.delParam(params.getCODE());
        return SUCCESS_TIP;
    }

}
